<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!--      <el-form-item label="故障地址" prop="address">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.address"-->
      <!--          placeholder="请输入故障地址"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="故障类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择故障类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属工厂" prop="deptId">
        <el-select
          v-model="queryParams.deptId"
          placeholder="请选择所属工厂"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="dept in deptOptions"
            :key="dept.deptId"
            :label="dept.name"
            :value="dept.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="故障时间">
        <el-date-picker
          :editable="false"
          v-model="daterangeFaultTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <!--      <el-form-item label="上报人" prop="reportUserId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.reportUserId"-->
      <!--          placeholder="请输入上报人"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:faultReport:add']"
          >上报</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['equipment:faultReport:edit']"-->
      <!--          >修改</el-button-->
      <!--        >-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['equipment:faultReport:remove']"-->
      <!--          >删除</el-button-->
      <!--        >-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['equipment:faultReport:export']"-->
      <!--          >导出</el-button-->
      <!--        >-->
      <!--      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="faultReportList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="故障设备" align="center" prop="productName" />
      <el-table-column
        label="故障类型"
        align="center"
        prop="type"
        :formatter="typeFormat"
      />
      <el-table-column label="所属工厂" align="center" prop="dept.deptName" />
      <el-table-column
        label="故障时间"
        align="center"
        prop="faultTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.faultTime, "{y}-{m}-{d} {h}:{i}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="故障状态"
        align="center"
        prop="status"
        :formatter="statusFormat"
      />
      <el-table-column label="上报人" align="center" prop="user.nickName" />
      <el-table-column
        label="操作"
        align="center"
        width="200px"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleLook(scope.row)"
            >查看</el-button
          >
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['equipment:faultReport:edit']"-->
          <!--            >修改</el-button-->
          <!--          >-->
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['equipment:faultReport:remove']"-->
          <!--            >删除</el-button-->
          <!--          >-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改故障管理信息对话框 -->
    <el-dialog
      fullscreen
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      v-myDialogDrag
      destroy-on-close
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="40">
          <el-col :span="openDisabled ? 18 : 24">
            <el-col :span="24">
              <el-form-item label="所属工厂" prop="deptId">
                <el-select
                  v-model="form.deptId"
                  placeholder="请选择所属工厂"
                  clearable
                  size="small"
                  :disabled="openDisabled"
                >
                  <el-option
                    v-for="dept in deptOptions"
                    :key="dept.deptId"
                    :label="dept.name"
                    :value="dept.deptId"
                  />
                </el-select>
                <!--                <el-input v-model="form.deptId" placeholder="请输入所属工厂" />-->
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="故障设备" prop="productId">
                <Dialog
                  @dialogChange="form.productId = $event.value"
                  :echoValue="form.productName"
                  parameter="rows"
                  value="id"
                  label="name"
                  search="code/name"
                  url="/basic/eqpInfo/list"
                  :column="['设备编号/code', '设备名称/name']"
                  :openDisabled="openDisabled"
                  placeholder="请选择内容"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="故障类型" prop="type">
                <el-select
                  v-model="form.type"
                  placeholder="请选择故障类型"
                  :disabled="openDisabled"
                >
                  <el-option
                    v-for="dict in typeOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="故障位置" prop="address">
                <el-input
                  v-model="form.address"
                  placeholder="请输入故障位置"
                  :disabled="openDisabled"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="故障时间" prop="faultTime">
                <el-date-picker
                  :editable="false"
                  clearable
                  :disabled="openDisabled"
                  size="small"
                  v-model="form.faultTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm"
                  format="yyyy-MM-dd HH:mm"
                  placeholder="选择故障时间"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="故障描述" prop="faultContent">
                <el-input
                  :disabled="openDisabled"
                  v-model="form.faultContent"
                  type="textarea"
                  placeholder="请输入内容"
                />
              </el-form-item>
            </el-col>
            <!--            <el-col :span="24">-->
            <!--              <el-form-item label="故障状态">-->
            <!--                <el-radio-group v-model="form.status">-->
            <!--                  <el-radio-->
            <!--                    v-for="dict in statusOptions"-->
            <!--                    :key="dict.dictValue"-->
            <!--                    :label="dict.dictValue"-->
            <!--                    >{{ dict.dictLabel }}</el-radio-->
            <!--                  >-->
            <!--                </el-radio-group>-->
            <!--              </el-form-item>-->
            <!--            </el-col>-->
          </el-col>
          <el-col :span="6" v-if="openDisabled">
            <el-timeline :reverse="reverse">
              <el-timeline-item
                v-for="(activity, index) in activities"
                :key="index"
              >
                {{ "[" + activity.nickName + "]" + activity.dealContent }}
              </el-timeline-item>
            </el-timeline>

            <!-- <el-timeline-item v-for="(activity, index) in activities" :key="index" :timestamp="activity.dealTime">
                {{ "[" + activity.nickName + "]" + activity.dealContent }}
              </el-timeline-item>
            </el-timeline> -->
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">上 报</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFaultReport,
  getFaultReport,
  delFaultReport,
  addFaultReport,
  updateFaultReport,
  getRecord,
} from "@/api/equipment/repairInfo/faultReport";
import { getFactoryList } from "@/api/equipment/common";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "FaultReport",
  components: { Treeselect },
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedEqpRepairRecord: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 故障管理信息表格数据
      faultReportList: null,
      // 故障处理记录表格数据
      eqpRepairRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 故障类型字典
      typeOptions: [],
      // 故障时间时间范围
      daterangeFaultTime: [],
      // 故障状态字典
      statusOptions: [
        // { dictLabel: "待上报", dictValue: "01" },
        // { dictLabel: "上报", dictValue: "02" },
      ],
      // 部门树选项
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        address: null,
        type: null,
        deptId: null,
        faultTime: null,
        status: null,
        reportUserId: null,
        productId: null,
        productName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productId: [{ required: true, message: "不能为空", trigger: "blur" }],
        type: [{ required: true, message: "不能为空", trigger: "blur" }],
        faultTime: [{ required: true, message: "不能为空", trigger: "blur" }],
        deptId: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
      visible: false,
      reverse: true,
      activities: [
        {
          nickName: "无用户",
          dealContent: "",
          dealTime: "",
        },
      ],
      options: {},
      column: [
        //表头数据 + 参数配置
        {
          prop: "deptId",
          label: "部门ID",
          isValue: true, //选中后要传入接口的字段
        },
        {
          prop: "deptName",
          label: "部门名称",
          isSearch: true, //可查询数据的字段
          isLabel: true, //选中后要传入接口的字段
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getDicts("fault_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getDicts("fault_status").then((response) => {
      this.statusOptions = response.data;
    });
    // this.getTreeselect();
    this.getFactory();
  },
  methods: {
    getFactory() {
      getFactoryList().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // /** 查询部门下拉树结构 */
    // getTreeselect() {
    //   treeselect().then((response) => {
    //     this.deptOptions = response.data;
    //   });
    // },
    /** 查询故障管理信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeFaultTime && "" != this.daterangeFaultTime) {
        this.queryParams.params["beginFaultTime"] = this.daterangeFaultTime[0];
        this.queryParams.params["endFaultTime"] = this.daterangeFaultTime[1];
      }
      listFaultReport(this.queryParams).then((response) => {
        this.faultReportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 故障类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 故障状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productId: null,
        productName: null,
        address: null,
        type: null,
        deptId: null,
        faultTime: null,
        faultContent: null,
        status: "0",
        stopInfoId: null,
        reportUserId: null,
        dispatchTime: null,
        dispatchUserId: null,
        dealUserId: null,
        dealTime: null,
        reasonSolution: null,
        checkTime: null,
        checkUserId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null,
        source: "01",
      };
      this.eqpRepairRecordList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.activities = [{ nickName: "", dealContent: "", dealTime: "" }];
      this.open = true;
      this.title = "添加故障管理信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getFaultReport(id).then((response) => {
        this.form = response.data;
        this.eqpRepairRecordList = response.data.eqpRepairRecordList;
        this.open = true;
        this.title = "修改故障管理信息";
      });
      getRecord(id).then((response) => {
        this.activities = response.rows;
      });
    },
    /** 查看按钮 */
    handleLook(row) {
      const id = row.id || this.ids;
      getFaultReport(id).then((response) => {
        this.form = response.data;
        this.eqpRepairRecordList = response.data.eqpRepairRecordList;
        this.open = true;
        this.title = "查看故障管理信息";
      });
      getRecord(id).then((response) => {
        this.activities = response.rows;
      });
      this.openDisabled = true;
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.eqpRepairRecordList = this.eqpRepairRecordList;
          if (this.form.id != null) {
            updateFaultReport(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.$confirm("上报后将无法修改，是否确定上报?", "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "error",
            })
              .then(() => {
                this.form.source = "01";
                return addFaultReport(this.form);
              })
              .then(() => {
                this.msgSuccess("上报成功");
                this.open = false;
                this.getList();
              })
              .catch(() => {});
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除故障管理信息编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delFaultReport(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 故障处理记录序号 */
    rowEqpRepairRecordIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 故障处理记录添加按钮操作 */
    handleAddEqpRepairRecord() {
      let obj = {};
      obj.userId = "";
      obj.nickName = "";
      obj.dealTime = "";
      obj.dealStatus = "";
      this.eqpRepairRecordList.push(obj);
    },
    /** 故障处理记录删除按钮操作 */
    handleDeleteEqpRepairRecord() {
      if (this.checkedEqpRepairRecord.length == 0) {
        this.$alert("请先选择要删除的故障处理记录数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.eqpRepairRecordList.splice(
          this.checkedEqpRepairRecord[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleEqpRepairRecordSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.eqpRepairRecord.clearSelection();
        this.$refs.eqpRepairRecord.toggleRowSelection(selection.pop());
      } else {
        this.checkedEqpRepairRecord = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "equipment/faultReport/export",
        {
          ...this.queryParams,
        },
        "故障上报" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>
<style lang="scss">
.el-select,
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
.vue-treeselect__control {
  background: rgba(45, 173, 231, 0.25) !important;
  border-radius: 1px;
}
</style>
